yjx.controller('homeController', ['$rootScope', '$scope', '$compile', '$timeout', '$fetch','modalService','wxShareService','$location','$state',function ($rootScope, $scope, $compile, $timeout, $fetch,modalService,wxShareService,$location,$state) {

    /*test以下*/
    function dd() {
        $timeout(function () {
            $scope.tempwx = "components/user/identity/identity.html";
            var html = '<div class="e-detail" ng-include="\'' + $scope.tempwx + '\'"></div>';
            var detailHtml = $compile(html)($scope);
            angular.element(document.querySelector('#m-detail')).append(detailHtml);//后排插入
            angular.element(document.querySelector('#m-detail')).show();
            $rootScope._dCount++;
        })
    }
     //dd();
    //alert(window.location.href.split('#')[0])
    //wxShareService.getConfig(window.location.href.split('#')[0]);
    //wxShareService.getConfig("http://www.wanglan.site/");
    //$rootScope.title = "我想给你发个链接";
    //$rootScope.desc = "嗯这个东西超好吃有营养";
    //$rootScope.imgUrl= "http://wx.qlogo.cn/mmopen/vi_32/ibytib2JRtibvnkl1iaaKLL3l5TtczMNicMtAVic0k2WRVDTmUzNldSVeVGGCbOoG4E1XnzA6Mich3FL5L5PcLsRAibibJA/0"

    console.log("========================this is home")
    /*test以上*/

    /*获取banner数据*/
    function getBanners() {
        $fetch.fetch(
            '/activity/getBanners',
            {
                params:{
                    userId:$rootScope.userId
                },
                cache:false,
            }
        ).then(function(data){
                $timeout(function() {
                    if(data && data.banners && data.banners.length >0 ) {
                        $scope.bannerList = data.banners.slice(0,5);/*只取5个banner*/
                        function c() {
                            for(var i  in   $scope.bannerList ) {
                                $scope.bannerList[i]['style'] = {
                                    "background-image":"url("+$scope.bannerList[i].bannerUrl+")"
                                    //"background-image":"url("+$scope.bannerList[i].bannerUrl+")"
                                }
                            }
                        };
                        c();
                    }
                    if(data && data.brokerage){
                        $rootScope.mbrokerage = data.brokerage.brokerage;
                        $rootScope.mbrokerage1 = data.brokerage.brokerage1;
                    }
                })
            }).catch(function(e){
                modalService.mainMsgError(e.msg)
            })
    }

    getBanners();
    /*获取推荐产品数据*/
    function getRecProduct() {
            $fetch.fetch(
                '/recommend/products',
                {
                    cache:false,
                }

            ).then(function(data){
                    $timeout(function() {
                        if(data && data.products) {
                            $scope.productList = data.products;
                        }
                    })
                }).catch(function(e){
                    modalService.mainMsgError(e.msg)
                })
    }
    getRecProduct();

    /*获取合作机构数据*/

    var pageNum=1,
        pageSize=12,
        total;
    function getRecOrgs() {
        $fetch.fetch(
            '/settled/suppliers'
        ).then(function(data){
                $timeout(function() {
                    if(data && data.suppliers) {
                        $scope.orgListAll = data.suppliers;
                        total = $scope.orgListAll.length%pageSize==0?$scope.orgListAll.length/pageSize:parseInt($scope.orgListAll.length/pageSize)+1;
                        $scope.orgList = $scope.orgListAll.slice(pageNum-1,pageNum*pageSize);

                    }
                })
            }).catch(function(e){
                modalService.maingError(e.msg)
            })
    }
    getRecOrgs();

    /*获取精品课程*/
    function getCourses() {
        $fetch.fetch(
            '/settled/course'
        ).then(function(data){
                $timeout(function() {
                    $scope.courList = data.course
                })

            }).catch(function(e){
                modalService.mainMsgError(e.msg);
            })
    }
    getCourses();
    //wxShareService.getConfig($location.absUrl());


    /*合作机构侧滑*/
    $scope.sliderOrg = function(isLeft){
            angular.element(document.getElementById('orgList')).removeClass('silderRightAni');
            angular.element(document.getElementById('orgList')).removeClass('silderLeftAni');
        $timeout(function(){
            if(isLeft){
                console.log("左侧滑")
                if(pageNum<total){
                    console.log(  document.getElementById('orgList'))
                    console.log(   angular.element(document.getElementById('orgList')))
                    angular.element(document.getElementById('orgList')).addClass('silderLeftAni')
                }
                pageNum = pageNum<total?pageNum+1:total;

            }else{
                if(pageNum >1){
                    console.log(  document.getElementById('orgList'))
                    console.log(   angular.element(document.getElementById('orgList')))
                    angular.element(document.getElementById('orgList')).addClass('silderRightAni')
                }
                console.log("youceshu")
                pageNum = pageNum>1?pageNum-1:1

            }
            //console.log("pagenUM===="+pageNum+"total======"+total)
            //$scope.orgList = $scope.orgListAll.slice((pageNum-1)*pageSize,pageNum*pageSize);
            $timeout(function(){
                $scope.orgList = $scope.orgListAll.slice((pageNum-1)*pageSize,pageNum*pageSize);
                //$scope.orgList = $scope.orgListAll.slice(12,34);
            })
        })
    }

    $scope.bannerGo = function(item){
        if(item.productList && item.productList.length == 1) {
            /*单个产品的活动直接打开产品详情页*/
            $state.go('productDetail',({id:item.productList[0].id}));
        }else{
            $state.go('bannerDetail',({id:item.id}))
        }
    }
}])